<template>
  <!-- 淘宝官方轮播商品库 -->
  <view class="nine_includ_postage">
    <Head :title="title" :transparent="false">
      <!-- <text slot="action">搜索</text> -->
    </Head>
    <!-- 一级类目 -->
    <view class="category_top">
      <scroll-view scroll-x="true" class="scroll" :scroll-left="scrollX" :scroll-with-animation="true">
        <block>
          <view v-for="(item, key, index) in materialIds" :key="index" @click="choosenCategory(materialIds[key], key, index)" class="item"
            :class="materialIds[key] === param.materialId ? 'active' : ''">
            <text class="title">{{key}}</text>
          </view>
        </block>
      </scroll-view>
    </view>
    <ItemListComp class="item_box" v-if="templateType === 0 && questionList.length > 0" :questionList="questionList" :userInfo="userInfo" />
    <ItemListComp1 class="item_box" v-if="templateType === 1 && questionList.length > 0" :questionList="questionList" :userInfo="userInfo" />
    <ItemListComp2 class="item_box" v-if="templateType === 2 && questionList.length > 0" :questionList="questionList" :userInfo="userInfo" />
    <view class="cu-load bg-grey" v-if="isLoad" :class="finished?'over':'loading'"></view>
    <ScrollToTop :scrollTop="scrollTop" />
  </view>
</template>

<script>
  import Head from '@/pages/pagecomponents/head'
  import ItemListComp from '@/pages/pagecomponents/itemlist'
  import ItemListComp1 from '@/pages/pagecomponents/itemlist1'
  import ItemListComp2 from '@/pages/pagecomponents/itemlist2'
  import ScrollToTop from '@/pages/pagecomponents/other/scrolltotop'
  export default {
    components: {
      Head,
      ItemListComp,
      ItemListComp1,
      ItemListComp2,
      ScrollToTop,
    },
    data() {
      return {
        title: '',
        templateType: 0, // 当前物料商品集合所使用的模板ID
        scrollX: 0, // scroll窗口向左偏移的距离
        questionList: [],
        materialIds: {},
        param: {
          pageId: 1,
          pageSize: 10,
          materialId: 26200, // 一级类目
        },
        isLoad: false,
        finished: false,
        scrollTop: 0, // 页面滚动高度
      }
    },
    computed:{
      userInfo(){
        return this.$store.state.userInfo
      },
    },
    onPageScroll(e) {
      // 滚动条滚动
      this.scrollTop = e.scrollTop
    },
    onPullDownRefresh() {
      // 下拉刷新
      this.getItemList('pullDown')
    },
    onReachBottom() {
      // 触底函数
      this.nextPaging()
    },
    onLoad(e) {
      this.materialIds = JSON.parse(e.materialIds)
      for (let key in this.materialIds){ // 获取第一个选中类目
        this.param.materialId = this.materialIds[key]
        break;
      }
      this.title = e.title
      this.templateType = Number(e.templateType)
      this.getItemList()
    },
    methods: {
      choosenCategory(materialId, key, index) {
        // 点击一级类目
        // #ifndef H5 || APP-PLUS
        getApp().mtj.trackEvent('category', { // 埋点统计
          category_name: key,
          user_id: this.userInfo.id || 0,
        });
        // #endif
      
        // 自动滚动 start
        let scrollX = 0;
        for(let keyValue in this.materialIds){
          if(keyValue === key){
            break
          }
          scrollX += keyValue.length * 14
        }
        this.scrollX = scrollX
        // 自动滚动 end
      
        this.param.materialId = materialId;
        this.questionList = []
        this.param.pageId = 1
        this.getItemList();
        uni.pageScrollTo({ // 滚动到顶部
          scrollTop: 0
        });
      },
      nextPaging() {
        // 下一页
        if (this.finished !== true) {
          this.param.pageId += 1
          this.getItemList()
        }
      },
      getItemList(type) {
        if (type === 'pullDown') {
          // 下拉刷新
          this.isLoad = true
          this.questionList = []
          this.param.pageId = 1
        }
        this.$api.user.materialList(this.param).then(res => {
          if (type === 'pullDown') {
            // 下拉刷新
            uni.stopPullDownRefresh();
          }
          this.isLoad = false
          if (res.data.list && res.data.list.length <= 0) {
            this.finished = true;
          }
          let imgList = res.data.list
          let questionList = this.questionList
          questionList = questionList.concat(imgList)
          this.questionList = questionList
        })
      },
    }
  }
</script>

<style scoped lang="scss">
  .nine_includ_postage {
    position: relative;
    min-height: 100%;
    padding: 140px 0 50upx;
    .category_top {
      position: fixed;
      top: 49px; // 防止出现空隙
      left: 0;
      right: 0;
      padding: 0 20upx;
      margin-top: var(--status-bar-height);
      z-index: 2000;
      background: #ffffff;
      height: 50px;
      .scroll {
        width: 100%;
        white-space: nowrap;
        .item {
          display: inline-block;
          line-height: 80upx;
          +.item {
            margin-left: 32upx;
          }
        }
        .active {
          color: #4b6c92;
          font-weight: 600;
          border-bottom: 2px solid #4b6c92;
        }
      }
    }
    .item_box {
      // padding: 0 20upx;
      min-height: 800px;
    }
    .cu-load::before {
      content: none
    }
  }
</style>
